﻿CREATE VIEW dbo.VIEW_PU_STATE_HISTORY
AS
SELECT        dbo.PRODUCT_UNIT.ID, dbo.PRODUCT_UNIT.NAME, dbo.CP_STAGE.START_DATE AS START_DATE_ORDERED, dbo.DIRECT.DATE AS END_DATE_ORDERED, 
                         dbo.DIRECT.DATE AS START_DATE_IN_PRODUCTION, dbo.GUILD_BILL.DATE AS END_DATE_IN_PRODUCTION, dbo.GUILD_BILL.DATE AS START_DAY_IN_STOCK, 
                         dbo.VAT_INVOICE.DATE AS END_DATE_IN_STOCK, dbo.VAT_INVOICE.DATE AS DATE_OF_SHIPMENT
FROM            dbo.PRODUCT_UNIT INNER JOIN
                         dbo.CP_STAGE ON dbo.PRODUCT_UNIT.CP_STAGE_ID = dbo.CP_STAGE.ID INNER JOIN
                         dbo.CALENDAR_PLAN ON dbo.CP_STAGE.CALENDAR_PLAN_ID = dbo.CALENDAR_PLAN.ID LEFT OUTER JOIN
                         dbo.DIRECT ON dbo.PRODUCT_UNIT.DIRECT_NUMBER = dbo.DIRECT.ID LEFT OUTER JOIN
                         dbo.GUILD_BILL ON dbo.PRODUCT_UNIT.GUILD_BILL_NUMBER = dbo.GUILD_BILL.ID LEFT OUTER JOIN
                         dbo.VAT_INVOICE ON dbo.PRODUCT_UNIT.VAT_INVOICE_ID = dbo.VAT_INVOICE.ID
WHERE        (dbo.PRODUCT_UNIT.TYPE = 1)